package com.qingyan.boot.manage;

import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.ServiceConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
import com.mybatisflex.core.util.StringUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author csp 2024-04-13
 */
public class BizGenerator  implements IGenerator {

    protected String templatePath;

    protected String entityWithBaseTemplatePath = "/templates/enjoy/biz.tpl";


    public BizGenerator() {
        this("/templates/enjoy/biz.tpl");
    }

    public BizGenerator(String templatePath) {
        this.templatePath = templatePath;
    }

    @Override
    public void generate(Table table, GlobalConfig globalConfig) {

        if (!globalConfig.isServiceGenerateEnable()) {
            return;
        }
        PackageConfig packageConfig = globalConfig.getPackageConfig();
        ServiceConfig bizConfig = (ServiceConfig) globalConfig.getCustomConfig().get("bizConfig");
        String sourceDir = StringUtil.isNotBlank(bizConfig.getSourceDir()) ? bizConfig.getSourceDir() : packageConfig.getSourceDir();
        String servicePackagePath = ((String) globalConfig.getCustomConfig().get("bizConfigPackage")).replace(".", "/");
        File serviceJavaFile = new File(sourceDir, servicePackagePath + "/" + table.buildServiceClassName() + ".java");
        if (serviceJavaFile.exists() && !bizConfig.isOverwriteEnable()) {
            return;
        }
        Map<String, Object> params = new HashMap<>(4);
        params.put("table", table);
        params.put("bizConfig", bizConfig);
        params.put("packageConfig", packageConfig);
        params.put("javadocConfig", globalConfig.getJavadocConfig());
        params.putAll(globalConfig.getCustomConfig());
        globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceJavaFile);
        System.out.println("Biz ---> " + serviceJavaFile);
    }



    @Override
    public String getTemplatePath() {
        return templatePath;
    }

    @Override
    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }
}
